package hsl.study.controller;

import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.content.Media;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.ai.ollama.api.OllamaModel;
import org.springframework.ai.ollama.api.OllamaOptions;
import org.springframework.util.MimeTypeUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

/**
 * @Description:
 * @Author: hsl
 * @CreateDate: 2025/8/1 11:28
 * @Version: 1.0
 */

@RestController
@RequestMapping("/multiModel")
public class MultiModelController {

    @Autowired
    private OllamaChatModel ollamaChatModel;

    @GetMapping("/chat1")
    public String chat1(@RequestParam(name = "msg",defaultValue = "介绍一下图片中的内容并用中文输出") String msg){

        // 1. 文件资源
        ClassPathResource classPathResource = new ClassPathResource("1.png");
        // 2. 构建多模态的用户信息
        UserMessage userMessage = UserMessage.builder()
                .text(msg)
                .media(new Media(MimeTypeUtils.IMAGE_PNG, (org.springframework.core.io.Resource) classPathResource))
                .build();

        // 3.构建多模态的提示词
        Prompt prompt = new Prompt(userMessage, OllamaOptions.builder().model(OllamaModel.LLAVA).build());

        // 4.调用大模型
        ChatResponse response = ollamaChatModel.call(prompt);
        String result = response.getResult().getOutput().getText();
        System.out.println("result = " + result);
        return result;
    }

    @GetMapping("/chat2")
    public Map<String,String> chat2(@RequestParam(name = "msg",defaultValue = "介绍一下图片中的内容并用中文输出") String msg){

        // 1. 文件资源
        ClassPathResource classPathResource = new ClassPathResource("1.png");
        // 2. 构建多模态的用户信息
        UserMessage userMessage = UserMessage.builder()
                .text(msg)
                .media(new Media(MimeTypeUtils.IMAGE_PNG, (org.springframework.core.io.Resource) classPathResource))
                .build();

        // 3.构建多模态的提示词
        Prompt prompt = new Prompt(userMessage, OllamaOptions.builder().model(OllamaModel.LLAVA).build());

        // 4.调用大模型
        ChatResponse response = ollamaChatModel.call(prompt);
        String result = response.getResult().getOutput().getText();
        System.out.println("result = " + result);
        return Map.of("result",result);
    }

    @GetMapping("/chat3")
    public String chat3(@RequestParam(name = "msg") String msg, MultipartFile file) throws IOException{

        // 将MultiPartFile转换成--> resource
        InputStreamResource imgResource = new InputStreamResource(file.getInputStream());
        // 构建多模态的用户信息
        UserMessage userMessage = UserMessage.builder()
                .text(msg)
                .media(new Media(MimeTypeUtils.IMAGE_PNG, (org.springframework.core.io.Resource) imgResource))
                .build();

        // 构建多模态的提示词
        Prompt prompt = new Prompt(userMessage, OllamaOptions.builder().model(OllamaModel.LLAVA).build());
        // 4.调用大模型
        ChatResponse response = ollamaChatModel.call(prompt);
        String result = response.getResult().getOutput().getText();
        System.out.println("result = " + result);
        return result;
    }

}
